import base64
from Crypto import Random
from Crypto.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5
from Crypto.PublicKey import RSA
import hashlib
 
# random_generator = Random.new().read
# rsa = RSA.generate(1024, random_generator)
 
# 生成私钥
# private_key = rsa.exportKey()
# with open('private.pem', 'w') as f:
#     f.write(private_key.decode('utf-8'))
 
# # 生成公钥
# public_key = rsa.publickey().exportKey()
# print(public_key)
# print(private_key)
# with open('public.pem', 'w') as f:
#     f.write(public_key.decode('utf-8'))

private_key = '''-----BEGIN RSA PRIVATE KEY-----\nMIICXQIBAAKBgQCf5UtATxMFbsnC1uRPe0sZj9yA5sQp+oY/wfgiI1phvrv6qYRC\nU6j7FpWWMl3Xn5jNpz0O4N7SxAV2OvNzdwvvKghwtnVBTa5YZM8giItYK2ZRHu3q\nosZ3DjqvToqTltx4gHPaf6jw0kIBaHYKpWrF8/oe2q6Janz0eYbm941VVQIDAQAB\nAoGACAaDfNPAjegrZCY9OThkQtZxGB7dD4lVmyt3x540JLik0+uVv0Qr31xuaiaW\n22Iqkn9CJeDHYQH55Imyxfkem2W4O5Rmg7ql+ZQE+Gzl6xttWEFwHEr2Z8LqRHc4\n90zAoxJreiGAHVpdRRBkoWK2niTyL8aKxxnFS9sYRrkSWdECQQC+uoG5vAlgXU5A\nEvT/UpxFTXSqu8Rzso7lGJ5tYzZbkg/fORtaz8gVGG6VdvMWOSx0vCQRd82Zzzuz\nSF+DeuLxAkEA1p2O2Xy7Sj5dBX2pQO8cXMWD83G75QzpPqfw+M4VdAEauot64uk9\n0XjEMr0qTS2aSIZLaIFTGpaZCV8OCcoQpQJAFXFSdFc3lqXmzj1TXgEjoIb8vtUt\nWPSSLB+AcDvfOJxLPIVnFeaWUAjXJeahnbX/0X/1biFR9CDgTV+tnVKLgQJBAJBc\nDQGIICcRidquqxGthQN2LIqtXzj0FVR6/OEvJkkQkR0Xw0Isp9NmC3shuPmPCi9s\nff6+ttq0oB4onTXCgK0CQQC2RfQB4oKFVLmeliHOYCV9CqfEq3/HDgnaBiCMvdLe\n2hHe7xoXNE8eEZPwPJGWBA+0oYE/QUXrjWpw5XHlLJ4n\n-----END RSA PRIVATE KEY-----'''
# private_key = '''-----BEGIN RSA PRIVATE KEY-----MIICXQIBAAKBgQCf5UtATxMFbsnC1uRPe0sZj9yA5sQp+oY/wfgiI1phvrv6qYRCU6j7FpWWMl3Xn5jNpz0O4N7SxAV2OvNzdwvvKghwtnVBTa5YZM8giItYK2ZRHu3qosZ3DjqvToqTltx4gHPaf6jw0kIBaHYKpWrF8/oe2q6Janz0eYbm941VVQIDAQABAoGACAaDfNPAjegrZCY9OThkQtZxGB7dD4lVmyt3x540JLik0+uVv0Qr31xuaiaW22Iqkn9CJeDHYQH55Imyxfkem2W4O5Rmg7ql+ZQE+Gzl6xttWEFwHEr2Z8LqRHc490zAoxJreiGAHVpdRRBkoWK2niTyL8aKxxnFS9sYRrkSWdECQQC+uoG5vAlgXU5AEvT/UpxFTXSqu8Rzso7lGJ5tYzZbkg/fORtaz8gVGG6VdvMWOSx0vCQRd82ZzzuzSF+DeuLxAkEA1p2O2Xy7Sj5dBX2pQO8cXMWD83G75QzpPqfw+M4VdAEauot64uk90XjEMr0qTS2aSIZLaIFTGpaZCV8OCcoQpQJAFXFSdFc3lqXmzj1TXgEjoIb8vtUtWPSSLB+AcDvfOJxLPIVnFeaWUAjXJeahnbX/0X/1biFR9CDgTV+tnVKLgQJBAJBcDQGIICcRidquqxGthQN2LIqtXzj0FVR6/OEvJkkQkR0Xw0Isp9NmC3shuPmPCi9sff6+ttq0oB4onTXCgK0CQQC2RfQB4oKFVLmeliHOYCV9CqfEq3/HDgnaBiCMvdLe2hHe7xoXNE8eEZPwPJGWBA+0oYE/QUXrjWpw5XHlLJ4n-----END RSA PRIVATE KEY-----'''
public_key = "-----BEGIN PUBLIC KEY-----MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCseJicE843uA5R8vsXTWuC6kql9e98rr3cBumr/9rB8osEe1bCg9twShP+Tg7h6hP/rFP9UCDOvFmmUlbMbwcI71retVpXn/lbzyenyqldzPe24557XDctuN2+gxdDbvEuaARu1ilP09OHnH6jv8vEDo2/xq3F/9AcP8vaAmq59wIDAQAB-----END PUBLIC KEY-----"
# with open('private1.pem', 'r') as f:
#     private_key = f.read()

# public_key = ''
# with open('public.pem', 'r') as f:
#     public_key = f.read()

server_id = '-----BEGIN PUBLIC KEY-----MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCseJicE843uA5R8vsXTWuC6kql9e98rr3cBumr/9rB8osEe1bCg9twShP+Tg7h6hP/rFP9UCDOvFmmUlbMbwcI71retVpXn/lbzyenyqldzPe24557XDctuN2+gxdDbvEuaARu1ilP09OHnH6jv8vEDo2/xq3F/9AcP8vaAmq59wIDAQAB-----END PUBLIC KEY-----'
# 创建md5对象
hl = hashlib.md5()
hl.update(server_id.encode(encoding='utf-8'))
print(hl.hexdigest())

# encrypted_data = 'Ul5J8PKy3cGLmBeF/e7D0+7zsjtsPsjCQTeFKxShTzxuaMCTI8e21Az1ghzoGbRxAre7cw4JfRYS9qUjvS8CTSeZDR8/XiRrZQJrDgU0OoawXB2iQ6+Td01KVVECzzEs6VQ8CCnVdcu3mTs5nT+O4U8MhH3YnWSPKll4FhDXW5E='
publicKey = RSA.import_key(public_key)
# 加密
data = "haoyichen"
cipher = Cipher_pkcs1_v1_5.new(publicKey)
encrypted_data = cipher.encrypt(data)
print(encrypted_data)

# 解密
privateKey = RSA.import_key(private_key)
cipher = Cipher_pkcs1_v1_5.new(privateKey)
# data = cipher.decrypt(encrypted_data)
random_generator = Random.new().read
data = cipher.decrypt(base64.b64decode(encrypted_data), None)
print(data.decode('utf8'))